package com.xgpro.mapper.studycentre;

import com.xgpro.domain.studycentre.entity.TeacherStudent;
import com.xgpro.domain.studycentre.vo.DaiTuTeaSimple;
import com.xgpro.domain.studycentre.vo.StuSimple;
import com.xgpro.domain.studycentre.vo.StudentBack;
import com.xgpro.domain.studycentre.vo.UserInfoTS;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

/**
 * @author tx
 * @version 1.0
 * @date 2019/8/28 21:30
 */
public interface TeacherAndStudentMapper extends Mapper<TeacherStudent> {

    /**
     * 通过导师id查询学生信息
     * @param teaId
     * @return
     */
    @Select("select t_user.id as userId,t_user.username as username,t_user.photo as photo,t_deptment.name as deptName,t_teacher_student.cancle_flag as cancleFlag from " +
            "t_user   left join t_deptment on t_user.dept_id=t_deptment.id left join t_teacher_student on t_user.id=t_teacher_student.stu_id " +
            "  where t_user.id in (select stu_id from t_teacher_student where tea_id=#{teaId} and status=1) ")
    List<StudentBack> selectStudentByTeaId(Integer teaId);

    /**
     * 查询个人信息
     * @param id
     * @return
     */
    @Select("select t_user.id as id,t_user.photo as photo,t_user.username as username,t_deptment.name as deptName,t_user.job as job," +
            "t_edu.edu_kind as edu,t_edu.major as major,t_user.job_title as jobTitle from t_user left join t_deptment on t_deptment.id=t_user.dept_id left join " +
            "t_edu on t_edu.id=t_user.edu_id where t_user.id=#{id}")
    UserInfoTS qryUserInfoById(Integer id);

    /**
     * 查询带徒导师的列表
     * @return
     */
    @Select("<script>" +
            "select t_daitu_tea.user_id as userId,t_daitu_tea.stu_num as stuNum,t_daitu_tea.introduce as introduce,t_user.username as username ,t_user.photo as photo from t_daitu_tea left join t_user on t_daitu_tea.user_id=t_user.id " +
            "where  " +
            " 1=1 " +
            " <if test=\"name != null and name!='' \">" +
            "  and t_user.username like CONCAT('%',#{name},'%') " +
            " </if> " +
            "</script>")
    List<DaiTuTeaSimple> qryTeaSimple(@Param("name") String name);

    /**
     * 根据状态查询徒弟信息
     * @param teaId
     * @param status
     * @return
     */
    @Select("select t_user.id as stuId,t_user.username as  stuName,t_deptment.name as deptName from t_user left join t_deptment on t_user.dept_id=t_deptment.id " +
            "where  t_user.id in (select stu_id from t_teacher_student where status=#{status} and tea_id=#(teaId))")
    List<StuSimple> qryStuSimpleByStatus(@Param("teaId") Integer teaId,@Param("status") Integer status);

    /**
     * 通过申请
     * @param stuId
     * @param teaId
     * @param status
     * @return
     */
    @Update("update t_teacher_student set status=#{status} where stu_id=#{stuId} and tea_id=#{teaId} and (select stu_num from t_daitu_tea where user_id=#{teaId})<3")
    Integer passStudent(@Param("stuId") Integer stuId,@Param("teaId") Integer teaId,@Param("status") Integer status);

    /**
     * 解除关系学生数
     * @param teaId
     * @return
     */
    @Select("select count(id) from t_teacher_student where tea_id=#{teaId} and cancle_flag=2")
    Integer countStudents(Integer teaId);
}